<?php
/***
*    This file is part of Marketorama.
*
*    Marketorama is free software: you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation, either version 3 of the License, or
*    (at your option) any later version.
*
*    Marketorama is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR *PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License
*    along with Marketorama.  If not, see <http://www.gnu.org/licenses/>.
***/

/**
* The controller class for the apps entity. 
* This class generates the controller urls for listing, creating, editing and deleting apps.
* 
**/
    class Apps extends Controller {

        /** The default member for the Apps Controller.
        *   This function generates a view listing the apps for the current user logged in
        *   @see AppsModel
        **/
        function index()
        {
            $this->load->model('AppsModel');
            // ADD WHERE
            $data['query']=$this->AppsModel->getMyApps();
            $header_data['title']='My Apps';
            $header_data['current']='MyApps';
            $this->load->view('template/login_needed');    
            $this->load->view('template/header', $header_data);
            $this->load->view('apps_view', $data);
            $this->load->view('template/footer');
        }
        /** 
        *   newApp The entry point for new app creation and validation. It's responsible for validating the
        *   data, performing the insertion and redirecting to the app view once finished.
        */
        function newApp()
        {
            $this->load->library('form_validation');
                
            $this->form_validation->set_rules('Name', 'Application Name', 'trim|required|callback_appNameExists');
            $this->form_validation->set_rules('Description', 'Description', 'trim|required');
            $this->form_validation->set_rules('Url', 'Url', 'trim|required|callback_appUrlExists');
            $this->form_validation->set_rules('Package', 'Package', 'trim|required');
            $this->form_validation->set_rules('Icon', 'Icon', 'trim|required');

            if ($this->form_validation->run() == FALSE)
            {
                $header_data['title']='New App';
                $header_data['current']='MyApps';
                
                $this->load->view('template/login_needed');    
                $this->load->view('template/header', $header_data);
                $this->load->view('newapp_view');
                $this->load->view('template/footer');    
            }else{
                $this->load->model('AppsModel');
                $this->AppsModel->insertApp($this->input->post('Name'),
                                             $this->input->post('Description'),
                                             $this->input->post('Url'),
                                             $this->input->post('Package'),
                                             $this->input->post('Icon'),
                                             $this->session->userdata('UserId'), //sets current user as owner
                                             TRUE); //public field. TODO: get from from.
                redirect('apps');
            }
            
        }
        /**
         * This controller function shows the private page for an app
         **/
        function view($url)
        {
		    $this->load->view('template/login_needed');
		    $this->load->model('AppsModel');
            $approw=$this->AppsModel->getApp($url);
            $header_data['current']='MyApps';
            if ($approw==null || ($approw->OwnerId) !=$this->session->userdata('UserId')) // The url doesn't exist
            {
				$header_data['title']='View App :: ERROR: The app doesn\'t exist';
				$this->load->view('template/header', $header_data);
				$errormsg["type"]="App";
				$errormsg["name"]=$url;
				$this->load->view('template/smart_404', $errormsg);
			}
			else //bingo
			{
				$header_data['title']='Marketorama :: '.$approw->Name;
				$this->load->view('template/header', $header_data);
				$this->load->view('app_view', $approw);			
			}
			$this->load->view('template/footer');
		}

        
        /**
         * This controller function shows the public page for an app
         **/
        function view_app($url)
        {
            $this->load->model('AppsModel');
            $approw=$this->AppsModel->getApp($url);
            $header_data['current']='';
            
            
            if ($approw==null || !$approw->Public) // The url doesn't exist
            {
				$header_data['title']='View App :: ERROR: The app doesn\'t exist';
				$this->load->view('template/header', $header_data);
				$errormsg["type"]="public App";
				$errormsg["name"]=$url;
				$this->load->view('template/smart_404', $errormsg);
			}
			else //bingo
			{
				$header_data['title']='Marketorama :: '.$approw->Name;
				$this->load->view('template/header', $header_data);
				$this->load->view('app_view', $approw);			
			}
			$this->load->view('template/footer');
		}
        
        
        
        
        function appNameExists ($name)
        {
            return $this->appValueExists ('Name', $name, 'appNameExists');
        }
        function appUrlExists ($url)
        {
            return $this->appValueExists ('Url', $url,'appUrlExists');
            echo($url);
        }
        function appValueExists ($field, $value,$callback)
        {
            $this->load->model('AppsModel');
            if ($this->AppsModel->valueExists($field, $value))
                return TRUE;
            else
            {
                $this->form_validation->set_message($callback, 'The %s should be unique. "'.$value.'" already exists.');
                return FALSE;
            }
        }
        
    }
?>
